package com.baseframe.tool.utils.data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EnvUtil {

    //解析
    public static String parsingStr(String str,Object...args){


        return parsingStr(str,"{}",args);
    }

    public static String parsingStr(String str,String patternStr,Object...args){


        Pattern pattern = buildPattern(patternStr);
        Matcher m = pattern.matcher(str);
        int i = 0;
        while (m.find()){
            String group = m.group();

//            String substring = group.substring(1, group.length() - 1);//去掉花括号

            if (args!=null&&i<args.length){
                str = str.replace(group,String.valueOf(args[i]));
            }
            i++;
        }


        return str;
    }

    public static String[] getEnvStr(String str){
//        List<String> envStrList = new ArrayList<>();
//        String pattern = "\\{[^}]*}";
//        Pattern p = Pattern.compile(pattern);
//        Matcher m = p.matcher(str);
//        while (m.find()){
//            String group = m.group();
//
//            String envStr = group.substring(1, group.length() - 1);//去掉花括号
//            envStrList.add(envStr);
//        }

//        return envStrList.toArray(new String[0]);

        return getEnvStr(str,"{}");
    }

    public static String[] getEnvStr(String str,String patternStr){
        List<String> envStrList = new ArrayList<>();
//        char[] charArray = pattern.toCharArray();
//        if(charArray.length<2){
//            throw  new RuntimeException("格式不合法~");
//        }
//        String prefix = String.valueOf(charArray[0]);
//        String suffix = String.valueOf(charArray[1]);
//
//        if("{".equals(prefix)){
//            prefix = "\\{";
//        }
//        pattern = prefix+"[^"+suffix+"]*"+suffix;
////        String pattern = "\\{[^}]*}";
//        Pattern pattern = Pattern.compile(pattern);
        Pattern pattern = buildPattern(patternStr);
        Matcher m = pattern.matcher(str);
        while (m.find()){
            String group = m.group();

            String envStr = group.substring(1, group.length() - 1);//去掉花括号
            envStrList.add(envStr);
        }

        return envStrList.toArray(new String[0]);
    }

    private static Pattern buildPattern(String patternStr){
        char[] charArray = patternStr.toCharArray();
        if(charArray.length<2){
            throw  new RuntimeException("格式不合法~");
        }

        String prefix = String.valueOf(charArray[0]);
        String suffix = String.valueOf(charArray[1]);
        if("{".equals(prefix)){
            prefix = "\\{";//转义
        }
        patternStr = prefix+"[^"+suffix+"]*"+suffix;

        return Pattern.compile(patternStr);

    }

    public static void main(String[] args) {

        String[] envStr = getEnvStr("您的验证码是{code}，{ttl}分钟内有效。如非本人操作，请忽略本短信。", "{}");

        System.out.println(Arrays.toString(envStr));

//        String s = parsingStr("/update/{data_day}", 20230710);
//        System.out.println(s);

    }
}
